//
// Created by 11510 on 2024/6/12.
//

#ifndef DEMO1_COMPLEX_H
#define DEMO1_COMPLEX_H
#include <iostream>
using std::endl;
using std::string;
using std::ostream;
using std::istream;

void Scanner(istream &is, int& num){
    while(is >> num, !is.eof()){
        if(is.bad()){
            printf("istream broken\n");
            return;
        }else if(is.fail()){
            is.clear();
            is.ignore(0x7fffffffffffffff, '\n');

            printf("please re input!\n");
        } else{
            break;
        }
    }
}

// 复数
class Complex{
private:
    int _real;
    int _image;


public:
    Complex(int real, int image):_real(real), _image(image){}


    /**
     * io stream 已经删除拷贝构造，所以要加引用
     *
     * 本质：complex.operator<<(cout) => complex << cout
     *
     * @param os
     * @return
     */
    friend ostream& operator<<(ostream& os, const Complex &complex);

    friend istream& operator>>(istream& is, Complex &complex);

    string toString() const{
        return std::to_string(_real) + " + " + std::to_string(_image) + "i";
    }

};

ostream& operator<<(ostream& os, const Complex &complex){
    os << complex.toString();
    return os;
}

// TODO 有bug，等会改
istream& operator>>(istream& is, Complex &rhs){
    printf("please input real\n");
    Scanner(is, rhs._image);
    printf("please input image\n");
    Scanner(is, rhs._real);
    return is;
}



#endif //DEMO1_COMPLEX_H
